﻿namespace IP_Package.GUI
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// matlap filters implementation
    /// </summary>
    public partial class MATLAPFilters : Form
    {
        /// <summary>
        /// MatLap Connect Operation object
        /// </summary>
        private Operations.MATLAP_operations.MATLAPOper matlapFilterOper = new Operations.MATLAP_operations.MATLAPOper();

        /// <summary>
        /// buffer of orignal image
        /// </summary>
        private Color[,] bufferOfImage;

        /// <summary>
        /// flag to known if it save in orginal or not
        /// </summary>
        private bool saveOrNot = false;

        /// <summary>
        /// result image
        /// </summary>
        private Bitmap resultImg;

        /// <summary>
        /// the type of the filter
        /// </summary>
        private string type;

        /// <summary>
        /// Initializes a new instance of the <see cref="MATLAPFilters"/> class.
        /// </summary>
        public MATLAPFilters()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Gets or sets a value indicating whether [save or not1].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [save or not1]; otherwise, <c>false</c>.
        /// </value>
        public bool SaveOrNot1
        {
            get { return this.saveOrNot; }
            set { this.saveOrNot = value; }
        }

        /// <summary>
        /// Gets or sets the buffer of image.
        /// </summary>
        /// <value>
        /// The buffer of image.
        /// </value>
        public Color[,] BufferOfImage
        {
            get { return this.bufferOfImage; }
            set { this.bufferOfImage = value; }
        }

        /// <summary>
        /// Gets or sets the result img.
        /// </summary>
        /// <value>
        /// The result img.
        /// </value>
        public Bitmap ResultImg
        {
            get { return this.resultImg; }
            set { this.resultImg = value; }
        }

        /// <summary>
        /// Sets the info of orignal image.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="image">The orginal image.</param>
        /// <param name="width">The width of orignal image.</param>
        /// <param name="height">The height of orignal image.</param>
        /// <param name="bufferOfImage">The buffer of orignal image.</param>
        internal void SetInfo(string type, Image image, string width, string height, Color[,] bufferOfImage)
        {
            this.picOrg.Image = image;
            this.LabWdth.Text = width;
            this.LabHth.Text = height;
            this.bufferOfImage = bufferOfImage;
            this.type = type;
        }

        /// <summary>
        /// Handles the Click event of the BtCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtCancel_Click(object sender, EventArgs e)
        {
            this.saveOrNot = false;
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the BtApply control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtApply_Click(object sender, EventArgs e)
        {
            try
            {
                this.matlapFilterOper.BufferOfImage = this.BufferOfImage;
                if (this.IdealRadioGroup.Checked == true)
                {
                    this.resultImg = this.matlapFilterOper.CalcuTOIMGPass(this.type, "Ideal", double.Parse(this.txtD0Value.Text), double.Parse(this.txtOrder.Text));
                }
                else if (this.GaussianRadioGroup.Checked == true)
                {
                    this.resultImg = this.matlapFilterOper.CalcuTOIMGPass(this.type, "Gaussian", double.Parse(this.txtD0Value.Text), double.Parse(this.txtOrder.Text));
                }
                else if (this.ButterWorthRadioGroup.Checked == true)
                {
                    this.resultImg = this.matlapFilterOper.CalcuTOIMGPass(this.type, "Butterworth", double.Parse(this.txtD0Value.Text), double.Parse(this.txtOrder.Text));
                }

                this.FilterPictureBox.Image = this.resultImg;
            }
            catch
            {
                if (this.picOrg.Image == null)
                {
                    MessageBox.Show("no image to make filter operation");
                }
                else
                {
                    MessageBox.Show("operation fail make sure your data");
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the BtSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtSave_Click(object sender, EventArgs e)
        {
            if (this.resultImg == null)
            {
                try
                {
                    this.matlapFilterOper.BufferOfImage = this.BufferOfImage;
                    if (this.IdealRadioGroup.Checked == true)
                    {
                        this.resultImg = this.matlapFilterOper.CalcuTOIMGPass(this.type, "Ideal", double.Parse(this.txtD0Value.Text), double.Parse(this.txtOrder.Text));
                    }
                    else if (this.GaussianRadioGroup.Checked == true)
                    {
                        this.resultImg = this.matlapFilterOper.CalcuTOIMGPass(this.type, "Gaussian", double.Parse(this.txtD0Value.Text), double.Parse(this.txtOrder.Text));
                    }
                    else if (this.ButterWorthRadioGroup.Checked == true)
                    {
                        this.resultImg = this.matlapFilterOper.CalcuTOIMGPass(this.type, "ButterWorth", double.Parse(this.txtD0Value.Text), double.Parse(this.txtOrder.Text));
                    }

                    this.FilterPictureBox.Image = this.resultImg;
                }  
                catch
                {
                    if (this.picOrg.Image != null)
                    {
                        MessageBox.Show("operation fail make sure your data");
                    }
                    else
                    {
                        MessageBox.Show("no image to make filter operation");
                    }
                }
            }

            if (this.FilterPictureBox.Image != null)
            {
                this.bufferOfImage = this.matlapFilterOper.NewBufferOfImage;
                this.saveOrNot = true;
                this.Close();
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the ButterWorthRadioGroup control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ButterWorthRadioGroup_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ButterWorthRadioGroup.Checked == true)
            {
                this.txtOrder.Visible = true;
                this.labelOrder.Visible = true;
            }
            else
            {
                this.txtOrder.Visible = false;
                this.labelOrder.Visible = false;
            }
        }
    }
}
